home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "EnumerateValues"
- 'Subroutine EnumerateRegValues by Jason Merlo 10/18/99
- 'This Sub is an example of how to read registry values without knowing
- 'their data type. It simply takes a predefined registry key and dumps
- 'all of its values to a text file in the form: "Value name", "Value".
-
- 'NOTE: This was tested on Windows NT 4.0 SP4. Other operating systems
- 'May behave differently.
-
- Option Explicit
-
- Dim Index As Long
- Dim BufferLength As Long
- Dim BufferLength2 As Long
- Dim Resp As Long
- Dim KeyHandle As Long
- Dim DataType As Long
- Dim DummyString As String * 100
- Dim DummyString2 As String
- Dim i As Integer
-
- Dim OutFileNum As Long
- Const FILE_PATH = "C:\TEMP\keydump.txt"
- Const REG_PATH = "SOFTWARE\Microsoft\Windows\CurrentVersion"
-
- 'Here's the trick: Since the RegEnumValue API function requires a variable
- 'of type BYTE as its return (lpData), we need to do something sneaky.
- 'Since we don't have pointers in VB, we define a Type (or structure)
- 'where the first item is a single byte, and the rest is a byte array.
- 'This way, we can pass the function the byte it needs and our array will
- 'get populated with the return, whatever its data type. This is because
- 'The variables defined within a Type are always stored consecutively
- 'in memory. Now all we need to know is how to parse the returned bytes
- 'to get the value we want.
-
- Private Type ByteArray
- FirstByte As Byte
- ByteBuffer(100) As Byte
- End Type
-
- 'We want a variable of the above type to play with.
- Dim RegValue As ByteArray
-
- Public Sub EnumerateRegValues()
-
- 'Open the output file.
- OutFileNum = FreeFile
- Open FILE_PATH For Output As OutFileNum
-
- Index = 0
- 'Open the key and get its handle.
- Resp = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_PATH, 0, KEY_READ, KeyHandle)
- Do While Resp = 0
- 'These initializations are *very* important. Without them, the API call will
- 'think you are trying to pass it something and return bad data.
- DataType = 0
- BufferLength = 100
- BufferLength2 = 100
- DummyString = ""
- RegValue.FirstByte = 0
- For i = 0 To 100
- RegValue.ByteBuffer(i) = 0
- Next
-
- 'Grab the value at this Index.
- Resp = RegEnumValue(KeyHandle, Index, DummyString, BufferLength, 0, DataType, RegValue.FirstByte, BufferLength2)
- 'See if we're out of values. Return code 259 means no more data.
- If Resp = 259 Then Exit Do
-
- 'Now we need to parse the ByteArray.
- 'for strings, we grab the chr$ values.
- Select Case DataType
-
- 'String data.
- Case REG_SZ, REG_EXPAND_SZ:
- DummyString2 = Chr$(RegValue.FirstByte)
- 'After the first byte, the rest are in the ByteBuffer.
- 'Subtract one for null termination, one for FirstByte,
- 'and one for the zero index.
- For i = 0 To BufferLength2 - 3
- DummyString2 = DummyString2 & Chr$(RegValue.ByteBuffer(i))
- Next i
-
- 'DWORD data.
- Case REG_DWORD:
- 'All dwords are four bytes long, so we will just convert to decimal:
- DummyString2 = Trim(Str(RegValue.FirstByte _
- + (256 * RegValue.ByteBuffer(0)) _
- + (65536 * RegValue.ByteBuffer(1)) _
- + (16777216 * RegValue.ByteBuffer(2))))
-
- 'Binary data.
- Case REG_BINARY:
- 'Just line up the bytes. No null termination here.
- DummyString2 = Trim(Hex(RegValue.FirstByte))
- For i = 0 To BufferLength2 - 2
- DummyString2 = DummyString2 & " " & Trim(Hex(RegValue.ByteBuffer(i)))
- Next i
-
- 'We won't worry about other data types here for this example.
- 'But you can add code to parse the byte array for any other
- 'needed data types. Just check the text file for the DataType
- 'after running the function and compare it to the API DIMs!
- Case Else:
- DummyString2 = "Unsupported data type: " & DataType
-
- End Select
-
- 'Now the Value name is in DummyString -- up to the first Chr$(0),
- 'and the value itself resides in DummyString2.
- Write #OutFileNum, Left(DummyString, (InStr(DummyString, Chr$(0))) - 1), DummyString2
-
- Index = Index + 1
-
- Loop
- 'Let's be tidy and clean up after ourselves.
- Resp = RegCloseKey(KeyHandle)
-
- 'Close the file.
- Close #OutFileNum
-
- End Sub
-
- Public Sub main()
-
- 'This could be modified to pass the function a path...
- Call EnumerateRegValues
-
- End Sub
-